package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.*;
import com.example.jdbcdemo.pojo.GroupIdMap;
import com.example.jdbcdemo.pojo.Order;
import com.example.jdbcdemo.pojo.Product;
import com.example.jdbcdemo.service.StatsSaleServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatsSaleServiceImpl implements StatsSaleServiceInterface {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GroupIdMapMapper groupIdMapMapper;
    @Autowired
    StorageMapper storageMapper;
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public List<Map<String, Object>> statSaleProductList(int productId) {
        Product product=productMapper.queryProductById(productId);
       List<Map<String,Object>> resultList=new LinkedList<>();
       List<Order> orderList= orderMapper.queryOrderByProductId(productId);//获取该商品的所有order
        for(Order order:orderList){
            int groupid=order.getGroupId();
            GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(groupid);
            if (groupIdMap.getGroupType()==1&&groupIdMap.getStatus()==2)//审核通过的销售单才会统计
            {
                Map<String,Object> tempMap=new LinkedHashMap<>();
                tempMap.put("customerId",order.getCustomerId());
                tempMap.put("customerName",customerMapper.queryCustomerById(order.getCustomerId()).getName());
                tempMap.put("storageId",order.getStorageId());
                tempMap.put("storageAddress",storageMapper.queryStorageById(order.getStorageId()).getAddress());
                tempMap.put("cost",product.getCost());//成本
                tempMap.put("price",order.getPrice());//单价
                tempMap.put("number",order.getNumber());//数量
                tempMap.put("profit",(order.getPrice()-product.getCost())*order.getNumber());//这一单的利润
                resultList.add(tempMap);
            }
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> statsTotalSaleProduct() {
        List<Product> productList=productMapper.queryProductList();
        List<Map<String,Object>> resultList=new LinkedList<>();
        for (Product product:productList)
        {
            Map<String,Object> tempMap=new LinkedHashMap<>();
            int saleNumber=0;//已销售量
            double salePrice=0;//已销售额
            double saleCost=0;//销售成本
            boolean flag=false;
            int productId=product.getId();
            List<Order> orderList= orderMapper.queryOrderByProductId(productId);//获取该商品的所有order
            for(Order order:orderList){
                int groupid=order.getGroupId();
                GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(groupid);
                if (groupIdMap.getGroupType()==1&&groupIdMap.getStatus()==2)//审核通过的销售单才会统计
                {
                   saleNumber+=order.getNumber();
                   salePrice+=order.getPrice()*order.getNumber();
                   saleCost+=order.getNumber()*product.getCost();
                   flag=true;
                }
            }
            if (flag){
                tempMap.put("productId",productId);
                tempMap.put("productName",product.getName());
                tempMap.put("saleNumber",saleNumber);
                tempMap.put("saleCost",saleCost);
                tempMap.put("salePrice",salePrice);
                resultList.add(tempMap);
            }
        }
        return resultList;
    }
}
